package com.spring5.testdemo;

import com.spring5.Book;
import com.spring5.Orders;
import com.spring5.User;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@DisplayName("Spring5单元测试：IOC创建对象")
public class TestSpring5 {

  @DisplayName("注入属性-外部bean")
  @Test
  public void testAdd() {
    // 1 加载spring配置文件
    BeanFactory context = new ClassPathXmlApplicationContext("bean1.xml");

    // 2 获取配置创建的对象
    User user = context.getBean("user", User.class);

    System.out.println(user);
    user.add();
  }

  @DisplayName("set方式进行属性注入")
  @Test
  public void testBook1() {
    // 1 加载spring配置文件
    ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

    // 2 获取配置创建的对象
    Book book = context.getBean("book", Book.class);

    System.out.println(book);
    book.testDemo();
  }

  @DisplayName("有参构造器进行属性注入")
  @Test
  public void testOrders() {
    // 1 加载spring配置文件
    ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

    // 2 获取配置创建的对象
    Orders orders = context.getBean("orders", Orders.class);

    System.out.println(orders);
    orders.ordersTest();
  }
}
